package com.killer.edu.boss.controller;

import com.killer.edu.ad.api.AdRemoteService;
import com.killer.edu.ad.api.dto.PromotionAdDTO;
import com.killer.edu.ad.api.dto.PromotionSpaceDTO;
import com.killer.edu.common.result.ResponseDTO;
import com.killer.edu.common.result.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Killer
 * @date ：Created in 21-5-21 上午10:10
 * @description：${description}
 * @modified By：
 * @version: version
 */
@RestController
@RequestMapping("/ad")
public class AdController {

    @Autowired
    private AdRemoteService adRemoteService;

    @RequestMapping("/space/getAllSpaces")
    public ResponseDTO getAllSpace(){
        List<PromotionSpaceDTO> dtoList=adRemoteService.getAllSpaces();
        return ResponseDTO.success(dtoList);
    }

    @GetMapping("/getAllAds")
    public ResponseDTO<List<PromotionSpaceDTO>> getAllAds(@RequestParam("spaceKeys") String[] spaceKeys){
        List<PromotionSpaceDTO> allAds = adRemoteService.getAdBySpaceKey(spaceKeys);
        if(allAds.size() == 0){
            return ResponseDTO.response(ResultCode.SUCCESS, null);
        }
        return ResponseDTO.response(ResultCode.SUCCESS, allAds);
    }

    @ApiOperation(value = "获取所有的广告列表")
    @GetMapping("/getAdList")
    public ResponseDTO<List<PromotionAdDTO>> getAdList(){
        List<PromotionAdDTO> allAds = adRemoteService.getAllAds();
        if(allAds.size() == 0){
            return ResponseDTO.response(ResultCode.SUCCESS, null);
        }
        return ResponseDTO.response(ResultCode.SUCCESS, allAds);
    }


    @ApiOperation(value = "新增或者修改广告信息")
    @PostMapping("/saveOrUpdate")
    public ResponseDTO saveOrUpdateAd(@RequestBody PromotionAdDTO promotionAdDTO){
        return adRemoteService.saveOrUpdateAd(promotionAdDTO);
    }

    @ApiOperation(value = "根据Id获取广告信息")
    @GetMapping("/getAdById")
    public ResponseDTO<PromotionAdDTO> getAdById(@RequestParam("id") Integer id){
        PromotionAdDTO promotionAdDTO = adRemoteService.getAdById(id);
        if (promotionAdDTO == null){
            return ResponseDTO.response(ResultCode.SUCCESS,null);
        }
        return ResponseDTO.response(ResultCode.SUCCESS,promotionAdDTO);
    }


    @ApiOperation(value = "获取所有的广告位")
    @GetMapping("/space/getAllSpaces")
    public ResponseDTO<List<PromotionSpaceDTO>> getAllSpaces(){
        List<PromotionSpaceDTO> allSpaces = adRemoteService.getAllSpaces();
        if(allSpaces.size() == 0){
            return ResponseDTO.response(ResultCode.SUCCESS, null);
        }
        return ResponseDTO.response(ResultCode.SUCCESS, allSpaces);
    }


    @ApiOperation(value = "新增或者修改广告位")
    @PostMapping("/space/saveOrUpdate")
    public ResponseDTO saveOrUpdateSpace(@RequestBody PromotionSpaceDTO promotionSpaceDTO){
        return adRemoteService.saveOrUpdateSpace(promotionSpaceDTO);
    }


    @ApiOperation(value = "根据Id获取广告位")
    @GetMapping("/space/getSpaceById")
    public ResponseDTO<PromotionSpaceDTO> getSpaceById(@RequestParam("id") Integer id){
        PromotionSpaceDTO promotionSpaceDTO = adRemoteService.getSpaceById(id);
        if(promotionSpaceDTO == null){
            return ResponseDTO.response(ResultCode.SUCCESS,null);
        }
        return ResponseDTO.response(ResultCode.SUCCESS,promotionSpaceDTO);
    }
}
